<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Coupon extends Controller_Index_Base
{ 
    public function action_index()
    {
        $coupon_id = $this->request->param('id') ? $this->request->param('id') : null;
        
        $coupon = ORM::factory('coupon', $coupon_id);
        if (!$coupon->loaded()) {
            throw new Model_Exception('Данного купона в нашей базе не существует');
        }
        $shop   = ORM::factory('shop', $coupon->shop_id);
        
        $content = new View('index/coupon/index_tpl');
        $content->coupon = $coupon; 
        $content->shop   = $shop; 

        $title       = 'Промокод "'.$coupon->name.'" от '.$shop->name;
        $description = 'Воспользуйтесь промокодом '.$coupon->name.' от '.$shop->name.', чтобы немного сэкономить';
        $this->print_template($title, $content, $description);
    }
    
    public function action_shop()
    {
        $shop_url = $this->request->param('shop') ? $this->request->param('shop') : null;
        $page     = $this->request->param('page') ? $this->request->param('page') : 1; 
        
        $shop    = ORM::factory('shop')->get_shop_by_url($shop_url);
        if (!$shop->loaded()) {
            throw new Model_Exception('Данного магазина в нашей базе не существует');
        }      
        $coupons = ORM::factory('coupon')->get_coupons(null, null, $shop, $page);       
        
        $content = new View('index/coupon/shop_tpl');
        $content->shop    = $shop; 
        $content->coupons = $coupons;
        
        $count = ORM::factory('coupon')->get_coupons(null, null, $shop, $page, true);
        $content->pagination = array(                                                   
            'page' => $page,
            'per_page' => Model_Coupon::get_coupons_per_page(),
            'count' => $count,
        );

        $title       = 'Промокоды '.$shop->name.', бесплатные купоны и акции';
        $description = 'Воспользуйтесь бесплатными купонами и промокодами '.$shop->name.', чтобы немного сэкономить';
        $this->print_template($title, $content, $description);
    }
   
    public function action_go()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0;
        
        $coupon   = ORM::factory('coupon', $coupon_id);         
        
        $content = new View('index/coupon/go_tpl');
        $content->coupon  = $coupon;
        
        $this->response->body($content);    
    }
    
    public function action_head()
    {
        $coupon_id  = $this->request->param('coupon_id') ? $this->request->param('coupon_id') : 0; 
            
        $coupon   = ORM::factory('coupon', $coupon_id);
       
        $content = new View('index/coupon/head_tpl');
        $content->coupon  = $coupon;

        $this->response->body($content); 
    }
    
    public function action_new()
    {
        header('Location: /');
        die(); 
    }
} 
